# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.LeveLDao import LeveLDaoYM
import time
from . import app

localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

CORS(app, supports_credentials=True)

"""
表名：zzh_LeveL

说明：省份表
"""


@app.route('/queryLeveLById', methods=['POST'])
def queryLeveLByIdNoToken():
    """
    查询userRecharge
    :return:
    """
    if not request.json:
        abort(400)
    id = request.json.get("id")
    try:
        total_count, userCodeJSON = LeveLDaoYM().queryLeveLById(id)
        if total_count:
            return jsonify({"message": {"code": 1, "status": "queryLeveL OK"}, "info": userCodeJSON})
        else:
            return jsonify({"info": [], "message": {"code": 1, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})


@app.route('/queryLeveLByCondition', methods=['POST'])
def queryLeveLByConditionNoToken():
    """
    添加admin信息
    :return:
    """
    if not request.json:
        abort(400)
    LeveLName = request.json.get("LeveLName")
    pageIndex = request.json.get("pageIndex", 1)  # 传递页面索引号，默认为1
    pageSize = request.json.get("pageSize",10)
    try:
        total_count, UserCodeJSON = LeveLDaoYM().queryLeveLByCondition(LeveLName=LeveLName, pageIndex=pageIndex,pageSize=pageSize)
        if UserCodeJSON:
            return jsonify({"message": {"code": 1, "status": "queryLeveL OK"},"total":total_count, "info": UserCodeJSON})
        else:
            return jsonify({"info": [], "message": {"code": 1, "status": "No data"}})

    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})


@app.route('/insertLeveL', methods=['POST'])
def insertLeveLNoToken():
    """
    添加zzh_user_login_log信息
    :return:
    """
    if not request.json:
        abort(400)

    Info = {

        'LeveLName': request.json.get('LeveLName'),  #
        'LeveLCode': request.json.get('LeveLCode')  #

    }
    if not Info:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    try:
        rs = LeveLDaoYM().insertLeveL(Info)
        if rs:
            return jsonify(
                {"massage": {"code": 1, "info": "addLeveL OK"}})  # 添加函数调用成功，返回添加的信息（不包括userRecharge的Id）
        else:
            return jsonify({"message": {"code": 0, "info": "addLeveL Error"}})  # 添加函数调用失败
    except Exception, e:
        return jsonify({"message": {"code": 0, "info": str(e)}})



@app.route('/updateLeveL', methods=['POST'])
def updateLeveLNoToken():  #

    # 更新zzh_user_recharge信息
    # :return:

    if not request.json:
        abort(400)

    Info = {
        'LeveLName': request.json.get('LeveLName'),  #
        'LeveLCode': request.json.get('LeveLCode'),  # #
        'id': request.json.get('id')  #

    }
    if not Info:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    try:
        rs = LeveLDaoYM().updateLeveL(Info)
        if rs:
            return jsonify(
                {"addInfo": Info, "massage": {"code": 1, "info": "updateLeveL OK"}})  # 添加函数调用成功，返回添加的信息
        else:
            return jsonify({"message": {"code": 0, "info": "updateLeveL Error"}})  # 添加函数调用失败
    except Exception, e:
        return jsonify({"message": {"code": 0, "info": str(e)}})



@app.route('/deleteLeveL', methods=['POST'])  # 根据id数组删除zzh_user_Code表指定记录,传入格式为 "ids":[1,2,3,4]
def deleteLeveLNoToken():
    ids = request.json.get("ids")
    if not ids:
        return jsonify({"message": "请输入待删除的  id数组！"})
    try:
        rs = LeveLDaoYM().deleteLeveL(ids)
        if rs:
            return jsonify({"message": {"code": 1, "info": "deleteLeveL OK"}})
        else:
            return jsonify({"message": {"code": 0, "info": "deleteLeveL Error"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})
